package com.xmy.cultivate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.Patriarch;
import com.xmy.cultivate.respons.QyWeChartPushRs;
import com.xmy.cultivate.service.IMqPushService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import com.xmy.cultivate.util.ResultCode;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/weiChart")
public class WeiChartController {
    //@Autowired
    //private UserInfoService userInfoService;

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    IMqPushService iMqPushService;

    @GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl, HttpServletRequest request) {
        /*String redirectURL = wxMpService.oauth2buildAuthorizationUrl(userInfoUrl,
                WxConsts.OAuth2Scope.SNSAPI_BASE,
                URLEncoder.encode(returnUrl.replace("guiguketan", "#")));*/
        //returnUrl = ""+returnUrl;
        String redirectURL  = wxMpService.getOAuth2Service().buildAuthorizationUrl(returnUrl,WxConsts.OAuth2Scope.SNSAPI_USERINFO,null);
        return "redirect:" + redirectURL;
    }

    @GetMapping("/userInfo")
    public Result userInfo(@RequestParam("code") String code,
                           @RequestParam("state") String returnUrl) throws Exception {
        try {
            WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
            String openId = wxOAuth2AccessToken.getOpenId();
            System.out.println("openId:"+openId);
            /*QueryWrapper<Patriarch> patriarchQueryWrapper = new QueryWrapper<>();
            patriarchQueryWrapper.eq("openid",openId);
            Long count = iPatriarchService.count(patriarchQueryWrapper);
            String password = "123123";
            if (count.equals(0L)){
                Patriarch patriarch = new Patriarch();
                patriarch.setId(idWorker.nextId());
                patriarch.setOpenid(openId);
                iPatriarchService.save(patriarch);
            }
            String username = openId;
            password = new Md5Hash(password,username,3).toString();
            UsernamePasswordToken upToken = new UsernamePasswordToken(username,password);

            Subject subject = SecurityUtils.getSubject();
            //2.调用subject进行登录
            subject.login(upToken);//获取session

            String sid = (String) subject.getSession().getId();
            Map<String,Object> data = new HashMap<>();
            data.put("token",sid);
            //data.put("openId",openId);
            System.out.println("token:"+sid);*/
            //return new Result(Code.OK,"data","获取成功");

            String username = "admin";
            String password = "xmy1314999.";
            //System.out.print("username:"+username);
            //System.out.println();
            //System.out.print("password:"+password);
            password = new Md5Hash(password,username,3).toString();
            //System.out.print("password:"+password);
            UsernamePasswordToken upToken = new UsernamePasswordToken(username,password);
            //1.获取subject
            Subject subject = SecurityUtils.getSubject();
            //2.调用subject进行登录
            subject.login(upToken);//获取session
            String sid = (String) subject.getSession().getId();
            Map<String,Object> data = new HashMap<>();
            data.put("token",sid);
            //System.out.println("token:"+sid);
            return new Result(Code.OK,data,"获取成功");


        }catch (Exception e) {
            return new Result(ResultCode.MOBILEORPASSWORDERROR);
        }
    }

    @PostMapping("/qyWecharTest")
    public Result qyWecharTest(QyWeChartPushRs qyWeChartPushRs){
        //推送到企业微信
        iMqPushService.pushContentForQyWeChar(qyWeChartPushRs);
        return new Result(Code.OK,"","已发送到推送");
    }
}
